<template>
  <div class="login_containner">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单区域 -->
      <!--
        :model="LoginForm" 数据绑定
        :rules="LoginRules" 表单验证规则
        prop="username" 具体使用哪个验证规则
       -->
      <el-form
        label-width="0px"
        ref="LoginFormRef"
        class="login_form"
        :model="LoginForm"
        :rules="LoginRules"
      >
        <el-form-item prop="username">
          <!-- 用户名 -->
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="LoginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!-- 密码 -->
          <el-input
            prefix-icon="el-icon-s-home"
            type="password"
            v-model="LoginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="Login">登 录</el-button>
          <el-button type="info" @click="LoginReset">重 置</el-button>
        </el-form-item>
        <!-- 按钮区域 -->
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      LoginForm: {
        username: 'admin',
        password: '123456'
      },
      LoginRules: {
        username: [
          { required: true, message: '请输入用戶名', trigger: 'blur' },
          {
            min: 5,
            max: 12,
            message: '长度在 5 到 12 个字符',
            trigger: 'blur'
          }
        ],
        // trigger 失去焦点的时候验证
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 5,
            max: 12,
            message: '长度在 5 到 12 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    LoginReset() {
      // 一、使用v-model
      // this.LoginForm.username = ''
      // this.LoginForm.password = ''
      // 二、使用ui自带方法表单重置
      this.$refs.LoginFormRef.resetFields()
    },
    Login() {
      // 表单验证validate()
      this.$refs.LoginFormRef.validate(async (valid) => {
        if (valid) {
          const { data: result } = await this.$http.post('login', this.LoginForm)
          if (result.meta.status === 200) {
            this.$message.success('登录成功')
            console.log(result.data.token)
            // 保存token到sessionstorge
            window.sessionStorage.setItem('token', result.data.token)
            // 跳转页面到home
            this.$router.push('/home')
          } else {
            this.$message.error('登录失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.login_containner {
  background-color: #6386a8;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 350px;
  background-color: #fff;
  //开启定位 移动页面的百分之五十
  position: absolute;
  top: 50%;
  left: 50%;
  //移动盒子的50%
  transform: translate(-50%, -50%);
  //头像盒子区域
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    background-color: #fff;
    // top:-50%;
    transform: translate(-50%, -50%);
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.btns {
  // 弹性盒子模型
  display: flex;
  // 尾部对齐
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
</style>
